package com.richrail.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import com.richrail.console.CLI;
import com.richrail.console.ResponseListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ConsoleWindow extends JPanel implements Named, ResponseListener
{
	private static final long serialVersionUID = 1L;
	
	private JScrollPane outputScrollPane;
	
	private JTextPane outputPane;
	
	private TabbedView tabbedDisplay;
	
	private JButton executeButton;
	
	private JTextField commandField;
	
	private JLabel commandLabel;
	
	private CLI cli;
	
	private List<String> commandHistory = new ArrayList<String>();
	
	private int commandHistoryIndex = -1;
	
	private boolean tempCommandAdded = false;
	
	public ConsoleWindow(CLI cli)
	{
		super();
		this.cli = cli;
		initGUI();
	}
	
	private void initGUI()
	{
		try
		{
			this.setPreferredSize(new java.awt.Dimension(900, 600));
			this.setLayout(null);
			{
				outputScrollPane = new JScrollPane();
				this.add(outputScrollPane);
				outputScrollPane.setBounds(12, 343, 876, 212);
				{
					outputPane = new JTextPane();
					outputPane.setEditable(false);
					outputPane.setBackground(Color.BLACK);
					outputPane.setForeground(Color.WHITE);
					outputScrollPane.setViewportView(outputPane);
				}
			}
			{
				commandLabel = new JLabel();
				this.add(commandLabel);
				commandLabel.setText("command");
				commandLabel.setBounds(12, 570, 92, 15);
			}
			{
				commandField = new JTextField();
				this.add(commandField);
				commandField.setBounds(116, 567, 649, 22);
				commandField.addKeyListener(new KeyAdapter()
				{
					@Override
					public void keyPressed(KeyEvent evt)
					{
						commandFieldKeyPressed(evt);
					}
				});
			}
			{
				executeButton = new JButton();
				this.add(executeButton);
				executeButton.setText("execute");
				executeButton.setBounds(777, 567, 112, 22);
				executeButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent evt)
					{
						executeButtonActionPerformed(evt);
					}
				});
			}
			{
				tabbedDisplay = TabbedView.create();
				this.add(tabbedDisplay);
				tabbedDisplay.setBounds(12, 12, 876, 319);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void executeButtonActionPerformed(ActionEvent evt)
	{
		String command = commandField.getText();
		cli.execute(command);
		if (tempCommandAdded)
		{
			commandHistory.remove(commandHistory.size() - 1);
		}
		commandHistory.add(command);
		commandHistoryIndex = commandHistory.size();
		commandField.setText("");
	}
	
	@Override
	public void onResponse(String response)
	{
		outputPane.setText(outputPane.getText() + response + "\n");
	}
	
	private void commandFieldKeyPressed(KeyEvent evt)
	{
		if (evt.getKeyCode() == KeyEvent.VK_ENTER)
		{
			executeButton.doClick();
		}
		else if (evt.getKeyCode() == KeyEvent.VK_UP)
		{
			if (commandHistoryIndex > 0)
			{
				updateCommandText(commandHistoryIndex - 1);
			}
		}
		else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
		{
			if (commandHistoryIndex < commandHistory.size() - 1)
			{
				updateCommandText(commandHistoryIndex + 1);
			}
		}
	}
	
	private void updateCommandText(int index)
	{
		String command = commandHistory.get(index);
		if (command != null)
		{
			String currentCommand = commandField.getText();
			if (!currentCommand.isEmpty() && !currentCommand.equals(commandHistory.get(commandHistoryIndex)))
			{
				commandHistory.add(currentCommand);
				tempCommandAdded = true;
			}
			commandField.setText(command);
			commandHistoryIndex = index;
		}
	}
	
	@Override
	public String getName()
	{
		return "Console";
	}
}
